home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Devices / dev_examples / ClipDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  10.0 KB  |  483 lines

  1. /*
  2.  * Clipdemo.c
  3.  *
  4.  * Demonstrate use of clipboard I/O.  Uses general functions
  5.  * provided in cbio.c
  6.  *
  7.  * Compile with SAS C 5.10: LC -b1 -cfistq -v -y -L+cbio.o
  8.  *
  9.  * Run from CLI only
  10.  */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/ports.h>
  14. #include <exec/io.h>
  15. #include <exec/memory.h>
  16. #include <devices/clipboard.h>
  17. #include <libraries/dosextens.h>
  18. #include <libraries/dos.h>
  19.  
  20. #include "cb.h"
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/alib_protos.h>
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. #ifdef LATTICE
  30. int CXBRK(void) { return(0); }  /* Disable SAS CTRL/C handling */
  31. int chkabort(void) { return(0); }  /* really */
  32. #endif
  33.  
  34. #define FORGETIT 0
  35. #define READIT   1
  36. #define WRITEIT  2
  37. #define POSTIT   3
  38.  
  39. /* prototypes */
  40.  
  41. int ReadClip( void );           /* Demonstrate reading clipboard data    */
  42. int WriteClip( char * );        /* Demonstrate write to clipboard        */
  43. int PostClip( char * );         /* Demonstrate posting data to clipboard */
  44.  
  45. void main( USHORT, char **);
  46.  
  47. char message[] = "\
  48. \nPossible switches are:\n\n\
  49. -r            Read, and output contents of clipboard.\n\
  50. -w [string]   Write string to clipboard.\n\n\
  51. -p [string]   Write string to clipboard using the clipboard POST mechanism.\n\n\
  52.               The Post can be satisfied by reading data from\n\
  53.               the clipboard.  Note that the message may never\n\
  54.               be received if some other application posts, or\n\
  55.               performs an immediate write to the clipboard.\n\n\
  56.               To run this test you must run two copies of this example.\n\
  57.               Use the -p switch with one to post data, and the -r switch\n\
  58.               with another to read the data.\n\n\
  59.               The process can be stopped by using the BREAK command,\n\
  60.               in which case this example checks the CLIP write ID\n\
  61.               to determine if it should write to the clipboard before\n\
  62.               exiting.\n\n";
  63.  
  64.  
  65. void main(argc,argv)
  66. USHORT argc;
  67. char **argv;
  68. {
  69.  
  70. int todo;
  71. char *string;
  72.  
  73. todo = FORGETIT;
  74.  
  75. if (argc)     /* from CLI ? */
  76.     {
  77.  
  78.     /* Very simple code to parse for arguments - will suffice for
  79.      * the sake of this example
  80.      */
  81.  
  82.     if (argc > 1)
  83.        {
  84.        if (!(strcmp(argv[1],"-r")))
  85.            todo = READIT;
  86.        if (!(strcmp(argv[1],"-w")))
  87.            todo = WRITEIT;
  88.        if (!(strcmp(argv[1],"-p")))
  89.            todo = POSTIT;
  90.  
  91.        string = NULL;
  92.  
  93.        if (argc > 2)
  94.            string=argv[2];
  95.  
  96.        }
  97.  
  98.     switch (todo)
  99.             {
  100.  
  101.             case READIT:
  102.  
  103.                  ReadClip();
  104.                  break;
  105.  
  106.             case POSTIT:
  107.  
  108.                  PostClip(string);
  109.                  break;
  110.  
  111.             case WRITEIT:
  112.  
  113.                  WriteClip(string);
  114.                  break;
  115.  
  116.             default:
  117.  
  118.                  printf("%s",message);
  119.                  break;
  120.  
  121.             }
  122.     }
  123. }
  124.  
  125. /*
  126.  * Read, and output FTXT in the clipboard.
  127.  *
  128.  */
  129.  
  130. ReadClip()
  131. {
  132. struct IOClipReq *ior;
  133. struct cbbuf *buf;
  134.  
  135.  
  136. /* Open clipboard.device unit 0 */
  137.  
  138. if (ior=CBOpen(0L))
  139.     {
  140.  
  141.     /* Look for FTXT in clipboard */
  142.  
  143.     if (CBQueryFTXT(ior))
  144.         {
  145.  
  146.         /* Obtain a copy of the contents of each CHRS chunk */
  147.  
  148.         while (buf=CBReadCHRS(ior))
  149.               {
  150.               /* Process data */
  151.  
  152.               printf("%s\n",buf->mem);
  153.  
  154.               /* Free buffer allocated by CBReadCHRS() */
  155.  
  156.               CBFreeBuf(buf);
  157.               }
  158.  
  159.         /* The next call is not really needed if you are sure */
  160.         /* you read to the end of the clip.                   */
  161.  
  162.         CBReadDone(ior);
  163.         }
  164.     else
  165.         {
  166.         puts("No FTXT in clipboard");
  167.         }
  168.  
  169.     CBClose(ior);
  170.     }
  171.  
  172. else
  173.     {
  174.     puts("Error opening clipboard unit 0");
  175.     }
  176.  
  177. return(0L);
  178. }
  179.  
  180. /*
  181.  * Write a string to the clipboard
  182.  *
  183.  */
  184.  
  185. WriteClip(string)
  186. char *string;
  187. {
  188.  
  189. struct IOClipReq *ior;
  190.  
  191. if (string == NULL)
  192.     {
  193.     puts("No string argument given");
  194.     return(0L);
  195.     }
  196.  
  197. /* Open clipboard.device unit 0 */
  198.  
  199. if (ior = CBOpen(0L))
  200.     {
  201.     if (!(CBWriteFTXT(ior,string)))
  202.         {
  203.         printf("Error writing to clipboard: io_Error = %ld\n",ior->io_Error);
  204.         }
  205.     CBClose(ior);
  206.     }
  207. else
  208.     {
  209.     puts("Error opening clipboard.device");
  210.     }
  211.  
  212. return(0);
  213. }
  214.  
  215.  
  216. /*
  217.  * Write a string to the clipboard using the POST mechanism
  218.  *
  219.  * The POST mechanism can be used by applications which want to
  220.  * defer writing text to the clipboard until another application
  221.  * needs it (by attempting to read it via CMD_READ).  However
  222.  * note that you still need to keep a copy of the data until you
  223.  * receive a SatisfyMsg from the clipboard.device, or your program
  224.  * exits.
  225.  *
  226.  * In most cases it is easier to write the data immediately.
  227.  *
  228.  * If your program receives the SatisfyMsg from the clipboard.device,
  229.  * you MUST write some data.  This is also how you reply to the message.
  230.  *
  231.  * If your program wants to exit before it has received the SatisfyMsg,
  232.  * you must check the io_ClipID field at the time of the post against
  233.  * the current post ID which is obtained by sending the CBD_CURRENTWRITEID
  234.  * command.
  235.  *
  236.  * If the value in io_ClipID (returned by CBD_CURRENTWRITEID) is greater
  237.  * than your post ID, it means that some other application has performed
  238.  * a post, or immediate write after your post, and that you're application
  239.  * will never receive the SatisfyMsg.
  240.  *
  241.  * If the value in io_ClipID (returned by CBD_CURRENTWRITEID) is equal
  242.  * to your post ID, then you must write your data, and send CMD_UPDATE
  243.  * before exiting.
  244.  *
  245.  */
  246.  
  247. PostClip(string)
  248. char *string;
  249. {
  250.  
  251. struct MsgPort *satisfy;
  252. struct SatisfyMsg *sm;
  253. struct IOClipReq *ior;
  254. int mustwrite;
  255. ULONG postID;
  256.  
  257. if (string == NULL)
  258.     {
  259.     puts("No string argument given");
  260.     return(0L);
  261.     }
  262.  
  263. if (satisfy = CreatePort(0L,0L))
  264.     {
  265.  
  266.     /* Open clipboard.device unit 0 */
  267.  
  268.     if (ior = CBOpen(0L))
  269.         {
  270.         mustwrite = FALSE;
  271.  
  272.         /* Notify clipboard we have data */
  273.  
  274.         ior->io_Data    = (STRPTR)satisfy;
  275.         ior->io_ClipID  = 0L;
  276.         ior->io_Command = CBD_POST;
  277.         DoIO( (struct IORequest *) ior);
  278.  
  279.         postID = ior->io_ClipID;
  280.  
  281.         printf("\nClipID = %ld\n",postID);
  282.  
  283.         /* Wait for CTRL-C break, or message from clipboard */
  284.         Wait(SIGBREAKF_CTRL_C|(1L << satisfy->mp_SigBit));
  285.  
  286.         /* see if we got a message, or a break */
  287.         puts("Woke up");
  288.  
  289.  
  290.         if (sm = (struct SatisfyMsg *)GetMsg(satisfy))
  291.             {
  292.             puts("Got a message from the clipboard\n");
  293.  
  294.             /* We got a message - we MUST write some data */
  295.             mustwrite = TRUE;
  296.             }
  297.         else
  298.             {
  299.             /* Determine if we must write before exiting by
  300.              * checking to see if our POST is still valid
  301.              */
  302.  
  303.             ior->io_Command = CBD_CURRENTWRITEID;
  304.             DoIO( (struct IORequest *) ior);
  305.  
  306.             printf("CURRENTWRITEID = %ld\n",ior->io_ClipID);
  307.  
  308.             if (postID >= ior->io_ClipID)
  309.                 mustwrite = TRUE;
  310.  
  311.             }
  312.  
  313.         /* Write the string of text */
  314.  
  315.         if (mustwrite)
  316.             {
  317.             if (!(CBWriteFTXT(ior,string)))
  318.                 puts("Error writing to clipboard");
  319.             }
  320.         else
  321.             {
  322.             puts("No need to write to clipboard");
  323.             }
  324.  
  325.         CBClose(ior);
  326.         }
  327.     else
  328.         {
  329.         puts("Error opening clipboard.device");
  330.         }
  331.  
  332.     DeletePort(satisfy);
  333.     }
  334. else
  335.     {
  336.     puts("Error creating message port");
  337.     }
  338.  
  339. return(0);
  340. }
  341.  
  342.  
  343. /*
  344.  * Changehook_Test.c
  345.  *
  346.  * Demonstrate the use of CBD_CHANGEHOOK command.
  347.  * The program will set a hook and wait for the clipboard data to change.
  348.  * You must put something in the clipboard in order for it to return.
  349.  *
  350.  * Compile with SAS C 5.10: LC -cfist -v -y -L+Hookface.o+cbio.o
  351.  *
  352.  * Requires Kickstart 36 or greater.
  353.  *
  354.  * Run from CLI only
  355.  */
  356.  
  357. #include <exec/types.h>
  358. #include <exec/memory.h>
  359. #include <exec/ports.h>
  360. #include <exec/tasks.h>
  361. #include <exec/io.h>
  362. #include <devices/clipboard.h>
  363. #include <dos/dos.h>
  364. #include <utility/hooks.h>
  365. #include "cb.h"
  366.  
  367. #include <clib/macros.h>
  368. #include <clib/alib_protos.h>
  369. #include <clib/exec_protos.h>
  370.  
  371. #include <stdio.h>
  372. #include <string.h>
  373.  
  374.  
  375. LONG version = 1L;
  376.  
  377. extern ULONG SysBase, DOSBase;
  378.  
  379. /* Data to pass around with the clipHook */
  380. struct CHData
  381. {
  382.     struct Task *ch_Task;
  383.     LONG ch_ClipID;
  384. };
  385.  
  386. struct MsgPort *clip_port;
  387. struct Hook hook;
  388. struct CHData ch;
  389.  
  390. ULONG clipHook (struct Hook * h, VOID * o, struct ClipHookMsg * msg)
  391. {
  392. struct CHData *ch = (struct CHData *) h->h_Data;
  393.  
  394. if (ch)
  395.    {
  396.    /* Remember the ID of clip */
  397.    ch->ch_ClipID = msg->chm_ClipID;
  398.  
  399.    /* Signal the task that started the hook */
  400.    Signal (ch->ch_Task, SIGBREAKF_CTRL_E);
  401.    }
  402.  
  403. return (0);
  404. }
  405.  
  406. struct IOClipReq *OpenCB (LONG unit)
  407. {
  408. struct IOClipReq *clipIO;
  409.  
  410. /* Open clipboard unit 0 */
  411.  
  412. if (clipIO = CBOpen( 0L ))
  413.     {
  414.     ULONG hookEntry ();
  415.  
  416.     /* Fill out the IORequest */
  417.     clipIO->io_Data = (char *) &hook;
  418.     clipIO->io_Length = 1;
  419.     clipIO->io_Command = CBD_CHANGEHOOK;
  420.  
  421.     /* Set up the hook data */
  422.     ch.ch_Task = FindTask (NULL);
  423.  
  424.     /* Prepare the hook */
  425.     hook.h_Entry = hookEntry;
  426.     hook.h_SubEntry = clipHook;
  427.     hook.h_Data = &ch;
  428.  
  429.     /* Start the hook */
  430.     if (DoIO (clipIO))
  431.         printf ("unable to set hook\n");
  432.     else
  433.         printf ("hook set\n");
  434.  
  435.     /* Return success */
  436.     return ( clipIO );
  437.     }
  438.  
  439. /* return failure */
  440. return (NULL);
  441. }
  442.  
  443. void CloseCB (struct IOClipReq *clipIO)
  444. {
  445.  
  446. /* Fill out the IO request */
  447. clipIO->io_Data = (char *) &hook;
  448. clipIO->io_Length = 0;
  449. clipIO->io_Command = CBD_CHANGEHOOK;
  450.  
  451.     /* Stop the hook */
  452. if (DoIO (clipIO))
  453.     printf ("unable to stop hook\n");
  454. else
  455.     /* Indicate success */
  456.     printf ("hook is stopped\n");
  457.  
  458. CBClose(clipIO);
  459. }
  460.  
  461. main (int argc, char **argv)
  462. {
  463. struct IOClipReq *clipIO;
  464.  
  465. ULONG sig_rcvd;
  466.  
  467. printf ("Test v%ld\n", version);
  468.  
  469. if (clipIO=OpenCB (0L))
  470.     {
  471.     sig_rcvd = Wait ((SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_E));
  472.  
  473.     if (sig_rcvd & SIGBREAKF_CTRL_C)
  474.         printf ("^C received\n");
  475.  
  476.     if (sig_rcvd & SIGBREAKF_CTRL_E)
  477.         printf ("clipboard change, current ID is %ld\n", ch.ch_ClipID);
  478.  
  479.     CloseCB(clipIO);
  480.     }
  481. }
  482.  
  483.